package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.common.dtos.admin.AdminWmNewsDto;
import com.heima.model.common.dtos.admin.AuthDto;
import com.heima.model.common.dtos.common.ResponseResult;
import com.heima.model.common.dtos.wemedia.NewsUpOrDownDto;
import com.heima.model.common.dtos.wemedia.WmNewsDto;
import com.heima.model.common.dtos.wemedia.WmNewsPageReqDto;
import com.heima.model.common.vo.WmNewsVo;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 自媒体端文章相关接口
 */
@Api(value = "/api/v1/news", tags = {"自媒体端文章相关接口"})
@RestController
@RequestMapping("/api/v1/news")
public class WmNewController {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmUserService wmUserService;
    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;
    /**
     * 文章内容管理表
     * @param reqDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmNewsPageReqDto", name = "reqDto", value = "", required = true)
    })
    @ApiOperation(value = "文章内容管理表", notes = "文章内容管理表", httpMethod = "POST")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto reqDto){
        return wmNewsService.listNew(reqDto);
    }

    /**
     * 管理端文章审核列表
     * @param reqDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "AdminWmNewsDto", name = "reqDto", value = "", required = true)
    })
    @ApiOperation(value = "管理端文章审核列表", notes = "管理端文章审核列表", httpMethod = "POST")
    @PostMapping("/list_vo")
    public ResponseResult listVo(@RequestBody AdminWmNewsDto reqDto){
        return wmNewsService.listVo(reqDto);
    }

    /**
     * 根据文章id查询文章详情
     * @param newsId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "newsId", value = "", required = true)
    })
    @ApiOperation(value = "根据文章id查询文章详情", notes = "根据文章id查询文章详情", httpMethod = "GET")
    @GetMapping("/one_vo/{newsId}")
    public ResponseResult oneVo(@PathVariable("newsId") Long newsId){
        WmNewsVo wmNewsVo = new WmNewsVo();
        WmNews wmNews = wmNewsService.getById(newsId);
        BeanUtils.copyProperties(wmNews, wmNewsVo);
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getId, wmNews.getUserId());
        WmUser wmUser = wmUserService.getOne(wrapper);
        wmNewsVo.setAuthorName(wmUser.getName());
        return ResponseResult.okResult(wmNewsVo);
    }
    /**
     * 保存和修改文章功能
     * @param reqDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmNewsDto", name = "reqDto", value = "", required = true)
    })
    @ApiOperation(value = "保存和修改文章功能", notes = "保存和修改文章功能", httpMethod = "POST")
    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody WmNewsDto reqDto){
        return wmNewsService.submit(reqDto);
    }

    /**
     * 删除文章功能
     * @param newsId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "newsId", value = "", required = true)
    })
    @ApiOperation(value = "删除文章功能", notes = "删除文章功能", httpMethod = "GET")
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id")Integer newsId){
        return wmNewsService.delNews(newsId);
    }

    /**
     * 修改回显文章信息功能
     * @param newsId
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "newsId", value = "", required = true)
    })
    @ApiOperation(value = "修改回显文章信息功能", notes = "修改回显文章信息功能", httpMethod = "GET")
    @GetMapping("/one/{id}")
    public ResponseResult Show(@PathVariable("id")Integer newsId){
        return wmNewsService.show(newsId);
    }

    /**
     * 文章上下架功能
     * @param newsUpOrDownDto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "NewsUpOrDownDto", name = "newsUpOrDownDto", value = "", required = true)
    })
    @ApiOperation(value = "文章上下架功能", notes = "文章上下架功能", httpMethod = "POST")
    @PostMapping("down_or_up")
    public ResponseResult downOrUp(@RequestBody NewsUpOrDownDto newsUpOrDownDto){
        return wmNewsService.downOrUp(newsUpOrDownDto);
    }
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody AuthDto authDto){
        return wmNewsService.authPass(authDto);

    }
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody AuthDto authDto){
        return wmNewsService.authFail(authDto);

    }
}
